﻿/********************************************************************
 * *
 * * Copyright (C) 2013-2018 uiskin.cn
 * * 作者： BinGoo QQ：315567586 
 * * 请尊重作者劳动成果，请保留以上作者信息，禁止用于商业活动。
 * *
 * * 创建时间：2014-08-05
 * * 说明：
 * *
********************************************************************/

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Windows.Forms;

namespace NetWorkHelper.ICommond
{
    public  class CommonMethod
    {
        /// <summary>
        /// 域名转换为IP地址
        /// </summary>
        /// <param name="hostname">域名或IP地址</param>
        /// <returns>IP地址</returns>
        internal static string HostnameToIp(string hostname)
        {
            try
            {
                IPAddress ip;
                if (IPAddress.TryParse(hostname, out ip))
                    return ip.ToString();
                else
                    return Dns.GetHostEntry(hostname).AddressList[0].ToString();
            }
            catch
            {
                throw;
            }
        }

        /// <summary>  
        /// 获得用户IP  
        /// </summary>  
        public static string GetUserIp()
        {
            string ip;
            string[] temp;
            bool isErr = false;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
                ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            else
                ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
            if (ip.Length > 15)
                isErr = true;
            else
            {
                temp = ip.Split('.');
                if (temp.Length == 4)
                {
                    for (int i = 0; i < temp.Length; i++)
                    {
                        if (temp[i].Length > 3) isErr = true;
                    }
                }
                else
                    isErr = true;
            }

            if (isErr)
                return "1.1.1.1";
            else
                return ip;
        }
        /// <summary>  
        /// 提取开启代理/cdn服务后的客户端真实IP  
        /// </summary>  
        /// <returns></returns>  
        public static string GetTrueIP()
        {
            string ip = string.Empty;
            string X_Forwarded_For = HttpContext.Current.Request.Headers["X-Forwarded-For"];
            if (!string.IsNullOrWhiteSpace(X_Forwarded_For))
            {
                ip = X_Forwarded_For;
            }
            else
            {
                string CF_Connecting_IP = HttpContext.Current.Request.Headers["CF-Connecting-IP"];
                if (!string.IsNullOrWhiteSpace(CF_Connecting_IP))
                {
                    ip = CF_Connecting_IP;
                }
                else
                {
                    ip = HttpContext.Current.Request.UserHostAddress;
                }
            }
            return ip;
        }
        /// <summary>
        /// 服务器信息记录
        /// </summary>
        /// <param name="FileLog">记录地址</param>
        /// <param name="str">记录内容</param>
        internal static void FileOperate(string FileLog, string str)
        {
            if (FileLog == "")
                return;
            try
            {
                FileStream fs = new FileStream(FileLog, FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(str + DateTime.Now.ToString());
                sw.Close();
                fs.Close();
            }
            catch { throw; }
        }
        /// <summary>
        /// 外部调用是否需要用Invoket
        /// </summary>
        /// <param name="func">事件参数</param>
        internal static void EventInvoket(Action func)
        {
            Form form = null;
            if (Application.OpenForms.Count > 0)
            {
                form = Application.OpenForms[0];
            }
            try
            {

                //Form form = Application.OpenForms.Cast<Form>().FirstOrDefault();
                if (form != null && form.InvokeRequired)
                {
                    form.Invoke(func);
                }
                else
                {
                    func();
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// 具有返回值的 非bool 外部调用是否需要用Invoket
        /// </summary>
        /// <param name="func">方法</param>
        /// <returns>返回客户操作之后的数据</returns>
        internal static object EventInvoket(Func<object> func)
        {
            object haveStr;
            Form form = null;
            if (Application.OpenForms.Count > 0)
            {
                form = Application.OpenForms[0];
            }
            //Form form = Application.OpenForms.Cast<Form>().FirstOrDefault();
            if (form != null && form.InvokeRequired)
            {
                haveStr = form.Invoke(func);
            }
            else
            {
                haveStr = func();
            }
            return haveStr;
        }
        public delegate void Action();
        public delegate TResult Func<TResult>();

        public delegate TResult Func<T, TResult>(T a);

        public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);

        public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);

        public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
    }
}
